import api from "../../utils/api_util";
import baseUtil from "../../utils/base_util";
import tips from "../../utils/tip";

var app = getApp()
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        minusStatus: 'disabled',
        carts: [],
        hasList: true,
        totalPrice: 0,
        isDelete: false,
        selectAllStatus: false,
        ShopCartList: app.globalData.ShopCartList,
        //下单参数
        goodsId:'',
        quantity:'',
    },
    onShow: function () {
        this.getShopCarNumber();
        this.setData({
            goodsId:'',
            quantity:'',
            totalPrice:0,
        })
    },
    onLoad: function () {

    },

    //跳转页面
    jumpPage:function(e){
        let url =e.currentTarget.dataset.url;
        console.log('跳转地址:',url);
        if(this.data.goodsId && this.data.goodsId >0){
            baseUtil.pageJump(url+'?goodsId='+this.data.goodsId+'&quantity='+this.data.quantity)

        }else {
             tips.alert('请选择购买物品~')
        }
    },
    bindMinus: function (e) {
        console.log(e);
        var num = this.data.num;
        if (num > 1) {
            num--;
        }
        var minusStatus = num <= 1 ? 'disabled' : 'normal';
        this.setData({
            num: num,
            minusStatus: minusStatus
        });
    },
    bindPlus: function () {
        var num = this.data.num;
        num++;
        var minusStatus = num < 1 ? 'disabled' : 'normal';
        this.setData({
            num: num,
            minusStatus: minusStatus
        });
    },

    selectAll(e) {
        let selectAllStatus = this.data.selectAllStatus;
        selectAllStatus = !selectAllStatus;
        let ShopCartList = this.data.ShopCartList;
        for (let i = 0; i < ShopCartList.length; i++) {
            ShopCartList[i].checked = selectAllStatus;
        }
        this.setData({
            selectAllStatus: selectAllStatus,
            ShopCartList: ShopCartList
        });
        this.getTotalPrice();
    },
    closeModal(e) {
        this.setData({
            modalName: null
        })
    },
    deleteShop(e) {
        let ShopCartList = this.data.ShopCartList.filter(item => item.shopId != e.currentTarget.dataset.curshopid);
        this.setData({
            ShopCartList: ShopCartList,
            modalName: null
        });
        !ShopCartList.length ? this.setData({
            hasList: false
        }) : this.getTotalPrice();
    },

    /**
     购物车
     */

    //购物车列表
    getShopCarNumber() {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.listShoppingCart, {
            method: 'GET',
            data: {
                pageSize: 100,
                pageNumber: 1,
            },
            success: function (res) {
                res.data.list.forEach(item => {
                    item.checked = false
                })
                // if (!res.data.list.length) {
                //         that.data.hasList = false
                // } else {
                //     // that.getTotalPrice();
                // }
                that.setData({
                    ShopCartList: res.data.list,
                    // hasList: that.data.hasList,
                })
            },
        })
    },
    //计算总价
    getTotalPrice() {
        let ShopCartList = this.data.ShopCartList;
        let total = 0;
        for (let i = 0; i < ShopCartList.length; i++) {
            if (ShopCartList[i].checked) {
                total += ShopCartList[i].number * ShopCartList[i].price;
            }
        }
        this.setData({
            ShopCartList: ShopCartList,
            totalPrice: total.toFixed(2)
        });
    },
    //选择结算商品
    radioChange(e) {
        let that =this;
        console.log('id',e.detail.value)
        this.data.ShopCartList.forEach(item => {
            if (item.id == e.detail.value) {
                item.checked = true
                that.data.quantity = item.number
                that.data.goodsId = item.goodsId
            } else {
                item.checked = false
            }

        })
        console.log('数量',that.data.quantity)

        this.setData({
            ShopCartList: this.data.ShopCartList,
            goodsId: that.data.goodsId,
            quantity: that.data.quantity,
        });
        this.getTotalPrice();
    },

    // 增加数量
    addCount(e) {
        let that =this;
        const index = e.currentTarget.dataset.index;
        let ShopCartList = this.data.ShopCartList;
        let shopNum = ShopCartList[index].number;
        shopNum++;
        ShopCartList[index].number = shopNum;
        this.data.quantity = shopNum;
        console.log('数量',that.data.quantity);

        this.setData({
            ShopCartList: ShopCartList,
            quantity: that.data.quantity
        });

        this.getTotalPrice();
    },
    // 减少数量
    minusCount(e) {
        let that =this;

        const index = e.currentTarget.dataset.index;
        let ShopCartList = this.data.ShopCartList;
        let shopNum = ShopCartList[index].number;
        if (shopNum <= 1) {
            return false;
        }
        shopNum = shopNum - 1;
        ShopCartList[index].number = shopNum;
        this.data.quantity = shopNum;
        console.log('数量',that.data.quantity)

        this.setData({
            ShopCartList: ShopCartList,
            quantity: that.data.quantity

        });
        this.getTotalPrice();
    },
    //删除购物车商品
    isDelete() {
        this.setData({
            isDelete: !this.data.isDelete
        })
    },
    showModal(e) {
        let that =this;
        console.log('删除id:', e.target.id);
        wx.showModal({
            title: '提示',
            content: '确定从购物车删除该商品吗？',
            success(res) {
                if (res.confirm) {
                    api.wxRequest.apiRequest(that, api.urls.deleteShoppingCart, {
                        method: 'POST',
                        data: {
                            id: e.target.id,
                        },
                        success: function (res) {
                            that.getShopCarNumber();
                        },
                    })
                } else if (res.cancel) {
                }
            }
        })

    },
})